/**
 * FileName: GlobalExceptionHandler
 * Author: TangZaoBang
 * Date: 2023/04/29 13:57
 * Description:
 * <p>
 * History:
 * <author> TangZaoBang
 * <time> 2023/04/29 13:57
 * <version> 1.0.0
 * <desc> 版本描述
 */

package com.bosssoft.quickstart.demo.exception;

import com.bosssoft.quickstart.demo.pojo.vo.CommonResponse;
import com.bosssoft.quickstart.demo.utils.CommonResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

/**
 * @Class GlobalExceptionHandler
 * @Description 全局异常处理
 * @Author TangZaoBang
 * @Date 2023/04/29 13:57
 * @Version
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 异常
     * @author TangZaoBang
     * @date 2023/05/01 21:18
     * @param ex  ConstraintViolationException
     * @return com.bosssoft.quickstart.demo.pojo.vo.CommonResponse
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public CommonResponse<Exception> handleConstraintException(ConstraintViolationException ex){
        log.error("触发校验异常",ex);

        return  CommonResponseUtils.failed(BizErrorCode.PARAM_VALID_FIELD_REQUIRE);
    }

    @ExceptionHandler(NullPointerException.class)
    public CommonResponse<Exception> handleConstraintException(NullPointerException ex){
        log.error("触发异常空指针异常",ex);
        return CommonResponseUtils.failedWithMsg("0000","空指针异常");
    }


    @ExceptionHandler(BusinessException.class)
    public CommonResponse<Exception> handleBusinessException(BusinessException ex){
        log.error("触发业务异常",ex);
        return CommonResponseUtils.failedWithMsg(ex.getCode(),ex.getMessage());
    }


    /**
     * 处理其他所有的异常
     * @param ex 异常  demo重点 算术异常，SQL异常都归集这里处理
     * @return  公共报文模拟
     */
    @ExceptionHandler(Exception.class)
    public CommonResponse<Exception> handleConstraintException(Exception ex){
        log.error("其他类型异常",ex);
        return CommonResponseUtils.failedWithMsg("88888","其他类型异常:"+ex.getMessage());
    }
}
